package com.zhisheng.redis_hello.controller;

import com.zhisheng.redis_hello.entity.Student;
import com.zhisheng.redis_hello.service.StudentService;
import org.springframework.web.bind.annotation.*;
import java.util.Map;

@RestController
@RequestMapping("/students")
public class StudentController {

    private final StudentService studentService;

    // 构造注入服务
    public StudentController(StudentService studentService) {
        this.studentService = studentService;
    }

    /**
     * 新增学生
     * 请求示例：POST /students
     * 请求体：{"id":"1001","name":"张三","age":20,"className":"计算机一班"}
     */
    @PostMapping
    public String addStudent(@RequestBody Student student) {
        studentService.addStudent(student);
        return "学生信息添加成功";
    }

    /**
     * 根据ID查询学生
     * 请求示例：GET /students/1001
     */
    @GetMapping("/{id}")
    public Map<Object, Object> getStudent(@PathVariable String id) {
        return studentService.getStudentById(id);
    }
}